package com.xu.cloud.common.config;

import com.xu.cloud.common.advice.*;
import com.xu.cloud.common.advice.exception.DaoExceptionHandlerAdvice;
import com.xu.cloud.common.advice.exception.HttpExceptionHandlerAdvice;
import com.xu.cloud.common.advice.exception.JdkExceptionHandlerAdvice;
import com.xu.cloud.common.advice.exception.XuCloudExceptionHandlerAdvice;
import com.xu.cloud.common.desensitization.DesensitizationHandler;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.DataAccessException;

/**
 * 后置通知自动装配类
 *
 * @author xuguan
 * @since 2024/9/4
 */
@Configuration
public class XuCloudAdviceAutoConfiguration {

    @Configuration
    public static class RequestBodyAdviceConfig {
        @Bean
        @ConditionalOnMissingBean(name = {"requestBodyDecrypt", "requestBodyDecryptAdvice", "xuCloudRequestBodyDecryptAdvice"})
        public RequestBodyDecryptAdvice requestBodyDecryptAdvice() {
            return new RequestBodyDecryptAdvice();
        }
    }

    @Configuration
    public static class ResponseBodyAdviceConfig {
        @Bean
        @ConditionalOnMissingBean(name = {"desensitizationResponseBodyAdvice"})
        public DesensitizationResponseBodyAdvice desensitizationResponseBodyAdvice(DesensitizationHandler desensitizationHandler) {
            return new DesensitizationResponseBodyAdvice(desensitizationHandler);
        }

        @Bean
        @ConditionalOnMissingBean(name = {"responseBodyEncrypt", "responseBodyEncryptAdvice", "xuCloudResponseBodyEncryptAdvice"})
        public ResponseBodyEncryptAdvice responseBodyEncryptAdvice() {
            return new ResponseBodyEncryptAdvice();
        }

        @Bean
        @ConditionalOnMissingBean(name = {"wrapperResponseBody", "wrapperResponseBodyAdvice", "xuCloudWrapperResponseBodyAdvice"})
        @ConditionalOnProperty(prefix = "xu.cloud.response.wrapper", name = "enable", havingValue = "true", matchIfMissing = true)
        public XuCloudWrapperResponseBodyAdvice xuCloudWrapperResponseBodyAdvice() {
            return new XuCloudWrapperResponseBodyAdvice();
        }

        @Bean
        @ConditionalOnMissingBean(name = {"resultRequestAttributeSettingAdvice"})
        public ResponseRequestAttributeSettingAdvice resultRequestAttributeSettingAdvice() {
            return new ResponseRequestAttributeSettingAdvice();
        }

        @Bean
        @ConditionalOnMissingBean(name = {"errorCodeMsgResponseBodyAdvice"})
        public ErrorCodeMsgSettingAdvice errorCodeMsgResponseBodyAdvice() {
            return new ErrorCodeMsgSettingAdvice();
        }
    }

    @Configuration
    public static class ExceptionHandlerConfig {
        @Bean
        @ConditionalOnMissingBean(name = {"exceptionHandler", "exceptionHandlerAdvice", "xuCloudExceptionHandlerAdvice"})
        public XuCloudExceptionHandlerAdvice xuCloudExceptionHandlerAdvice() {
            return new XuCloudExceptionHandlerAdvice();
        }

        @Bean
        @ConditionalOnMissingBean(name = {"httpExceptionHandler", "httpExceptionHandlerAdvice", "xuCloudHttpExceptionHandlerAdvice"})
        public HttpExceptionHandlerAdvice httpExceptionHandlerAdvice() {
            return new HttpExceptionHandlerAdvice();
        }

        @Configuration
        @ConditionalOnClass(value = {DataAccessException.class})
        public static class DaoExceptionHandlerConfig {
            @Bean
            @ConditionalOnMissingBean(name = {"daoExceptionHandler", "daoExceptionHandlerAdvice", "xuCloudDaoExceptionHandlerAdvice"})
            public DaoExceptionHandlerAdvice daoExceptionHandlerAdvice() {
                return new DaoExceptionHandlerAdvice();
            }
        }

        @Bean
        @ConditionalOnMissingBean(name = {"jdkExceptionHandler", "jdkExceptionHandlerAdvice", "xuCloudJdkExceptionHandlerAdvice"})
        public JdkExceptionHandlerAdvice jdkExceptionHandlerAdvice() {
            return new JdkExceptionHandlerAdvice();
        }
    }
}
